package com.hxgz.steel.controller.portal;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.hxgz.steel.common.Const;
import com.hxgz.steel.common.ResponseCode;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.domain.*;
import com.hxgz.steel.dto.CategoryDto;
import com.hxgz.steel.dto.HomeProductDto;
import com.hxgz.steel.dto.HotStore;
import com.hxgz.steel.dto.MerchantDto;
import com.hxgz.steel.dto.NavigationDto;
import com.hxgz.steel.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Api(tags = "ProtalHomeController" ,description = "前台-首页")
@RestController
@RequestMapping("protalHome")
public class ProtalHomeController {
    @Autowired
    private UserMessageService userMessageService;
    @Autowired
    private ProductService productService;
    @Autowired
    private InformationService informationService;
    @Autowired
    private MerchantService mService;
    @Autowired
    private HomeService homeService;


    @RequestMapping(value = "announcementInfo.do",method = RequestMethod.POST)
    @ApiOperation("首页-公告")
    public ServerResponse< List<AnnouncementManager>> announcementInfo(){
        return userMessageService.announcementInfo();
    }

    /**
     *商品列表
     * @param page
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "productList.do",method = RequestMethod.POST)
    @ApiOperation(value = "商品列表",notes = "根据条件排序 :orderBy字段传 price desc(根据价格降序)，price asc(根据价格升序)，t3.salesCount desc(根据销量降序)，t3.salesCount asc(根据销量升序)," +
            "t1.create_time desc(根据时间倒序)，t1.create_time asc(根据时间升序)")
    public ServerResponse<PageInfo<List<HomeProductDto>>>  productList(Page page,
                                                                       @RequestParam(required = false) @ApiParam(value = "类别id") Integer categoryId,
                                                                       @RequestParam(required = false) @ApiParam(value = "商家id")Integer merchantId,
                                                                       @RequestParam(required = false) @ApiParam(value = "商品名称")String name,
                                                                       @RequestParam(required = false) @ApiParam(value = "商品名称id")Integer nameId,
                                                                       @RequestParam(required = false) @ApiParam(value = "品名id")Integer topNameId,
                                                                       @RequestParam(required = false) @ApiParam(value = "材质id")Integer materialsId,
                                                                       @RequestParam(required = false) @ApiParam(value = "特性id")Integer featuresId,
                                                                       @RequestParam(required = false) @ApiParam(value = "是否现货:1是，不传否")Integer spotGoods,
                                                                       @RequestParam(required = false) @ApiParam(value = "是否自营：1是，不传否")Integer self){

        Map<String,Object> paramMap = Maps.newHashMap();
        paramMap.put("categoryId",categoryId);//风水电消防
        paramMap.put("merchantId",merchantId);//店铺id
        paramMap.put("name",name);
        paramMap.put("nameId",nameId);
        paramMap.put("topNameId",topNameId);//品名id
        paramMap.put("materialsId",materialsId);//商品材质id
        paramMap.put("featuresId",featuresId);//特性id
        paramMap.put("spotGoods",spotGoods);//
        paramMap.put("self",self);
        return  productService.productList(page,paramMap);
    }


    @RequestMapping(value = "searchProductList.do",method = RequestMethod.POST)
    @ApiOperation(value = "搜索/排序",notes = "根据条件排序 :orderBy字段传 price desc(根据价格降序)，price asc(根据价格升序)，t3.salesCount desc(根据销量降序)，t3.salesCount asc(根据销量升序)," +
            "t1.create_time desc(根据时间倒序)，t1.create_time asc(根据时间升序),3个排序最多只能存在一个")
    public ServerResponse<PageInfo<List<HomeProductDto>>>  searchProductList(Page page, @RequestParam(required = false) @ApiParam(value = "类别id") Integer categoryId,
         @RequestParam(required = false) @ApiParam(value = "品名id")Integer topNameId,  @RequestParam(required = false) @ApiParam(value = "商品名称id")Integer nameId,
         @RequestParam(required = false) @ApiParam(value = "材质id")Integer materialsId,@RequestParam(required = false) @ApiParam(value = "特性id")Integer featuresId,
         @RequestParam(required = false) @ApiParam(value = "价格排序 1从高到低 0从低到高 不传默认排序")Integer hasPrice,@RequestParam(required = false) @ApiParam(value = "销量排序  1从高到低 0从低到高 不传默认排序")Integer hasSaleNum,
         @RequestParam(required = false) @ApiParam(value = "更新时间排序  1从近到远 0从远到近 不传默认排序")Integer hasUpdatetime,@RequestParam(required = false) @ApiParam(value = "是否现货:1是，不传否")Integer spotGoods,
         @RequestParam(required = false) @ApiParam(value = "是否自营：1是，不传否")Integer self,@RequestParam(required = false) @ApiParam(value = "是否仓储加工：1是，不传否")Integer isProcess){
        Map<String,Object> paramMap = Maps.newHashMap();
        paramMap.put("categoryId",categoryId);
        paramMap.put("topNameId",topNameId);//品名id
        paramMap.put("nameId",nameId);
        paramMap.put("materialsId",materialsId);//商品材质id
        paramMap.put("featuresId",featuresId);
        paramMap.put("hasPrice",hasPrice);
        paramMap.put("hasSaleNum",hasSaleNum);
        paramMap.put("featuresId",featuresId);
        paramMap.put("hasUpdatetime",hasUpdatetime);//
        paramMap.put("spotGoods",spotGoods);//
        paramMap.put("self",self);//
        paramMap.put("isProcess",isProcess);//
        return  productService.searchProductList(page,paramMap);
    }

    /**
     * 热销爆款
     * @param page
     * @return
     */
    @RequestMapping(value = "hotProduct.do",method = RequestMethod.POST )
    @ApiOperation("首页-热销爆款")
    public ServerResponse<PageInfo<List<HomeProductDto>>> hotProduct(Page page){
        return productService.hotProduct(page);
    }


    /**
     * 资讯列表
     * @param status
     * @return
     */
    @RequestMapping(value = "informationList.do",method = RequestMethod.POST)
    @ApiOperation("资讯列表")
    public ServerResponse<PageInfo<List<NewsManager>>> informationList(Page page,
    @ApiParam("类别,0风类，1水类，2电类,3消防类")@RequestParam Integer status){
       return informationService.informationList(page,status);

    }

    /**
     * 资讯详情
     * @param httpServletRequest
     * @param id
     * @return
     */
    @RequestMapping(value = "informationInfo.do",method = RequestMethod.POST)
    @ApiOperation("资讯详情")
    public ServerResponse<NewsManager> informationInfo(HttpServletRequest httpServletRequest,@RequestParam Long id){
        User user = Const.checkUserLogin(httpServletRequest);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return informationService.informationInfo(id);
    }


    @RequestMapping(value = "hotMerchant.do",method = RequestMethod.POST)
    @ApiOperation("首页-人气店铺")
    public ServerResponse<PageInfo<List<HotStore>>> hotMerchant(Page page){
        return mService.hotMerchant(page);
    }

    @ApiOperation("首页-banner图片和广告位")
    @RequestMapping(value = "banner.do",method = RequestMethod.POST)
    public ServerResponse<List<AdManager>> banner(){

        return homeService.banner();
    }
    @ApiOperation("app-首页banner")
    @RequestMapping(value = "bannerForApp.do",method = RequestMethod.POST)
    public ServerResponse<List<AdManager>> bannerForApp(){

        return homeService.getBannerList();
    }

    @ApiOperation("首页 -底部导航列表")
    @RequestMapping(value = "navigationList.do",method=RequestMethod.POST)
    public ServerResponse<List<NavigationDto>> navigationList(){

        ServerResponse serverResponse = homeService.navigationList();
        return serverResponse;
    }

    @ApiOperation("首页 -底部导航-详情")
    @RequestMapping(value = "navigationInfo.do",method=RequestMethod.POST)
    public ServerResponse<NavigationManager> navigationInfo(Long id){

        return homeService.navigationInfo(id);
    }
    @ApiOperation("全部商品分类-左侧主分类")
    @RequestMapping(value = "primeType.do",method = RequestMethod.POST)
    public ServerResponse<List<CategoryDto>> primeType(@ApiParam("页面大小")@RequestParam Integer pageSize){

        return  homeService.primeType(pageSize);
    }

    @ApiOperation(value="全部商品分类-右侧侧主分类")
    @RequestMapping(value = "childType.do",method = RequestMethod.POST)
    public ServerResponse<List<CategoryDto>> childType(@ApiParam(value = "主类id")@RequestParam(required = false) Integer id,
                                    @ApiParam("页面大小")@RequestParam Integer pageSize){
        return  homeService.childType(id,pageSize);
    }

    @ApiOperation(value="全部商品分类-最右侧商家")
    @RequestMapping(value = "getMerchantByKind.do",method = RequestMethod.POST)
    public ServerResponse<List<HotStore>> getMerchantByKind(@ApiParam(value = "分类id",required = true)@RequestParam Integer id,
                                    @ApiParam("页面大小")@RequestParam(required = false) Integer pageSize){
        return  homeService.getMerchantByKind(id ,pageSize);
    }

    @ApiOperation(value="商品详情-实单求购")
    @RequestMapping(value = "getMerchantMsg.do",method = RequestMethod.POST)
    public ServerResponse<MerchantDto> getMerchantMsg(HttpServletRequest httpServletRequest, @ApiParam("店铺id")@RequestParam(required = true)Long merchantId){
        User user = Const.checkUserLogin(httpServletRequest);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return mService.getMerchantById(merchantId);
    }


}
